<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\ArticleCate;
use app\common\model\Content;
use app\common\validate\VisitorComment;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\Request;
use think\exception\ValidateException;

class Comment extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {


        View::assign("title","文章列表");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        View::assign("title","文章发布");
        return \view();
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $user = $this->user;
        $d = $request->post();
        $d["contentid"] = $d["id"];

        unset($d["id"]);
        $d["status"] = 1;
        $d["uid"] = $user["id"];
        $d["userinfo"] = json_encode($user);

        if(!$user){
            //游客留言需要验证邮箱
            try {
                validate(VisitorComment::class)->check($d);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                $msg = $e->getError();
                return  json(["code"=>4000,"msg"=>$msg]);
            }
        }

        $mod = new \app\common\model\Comment();


        $res = $mod->save($d);

        if($res){
            //文章评论数自增
            Content::where("id",$d["contentid"])->inc("comment")->update();
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

    public function saveInfo()
    {
        return view();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        $article = Db::name("article")->find($id);
        Db::name("article")->where("id",$id)
            ->inc("hits")
            ->update();

        View::assign("article",$article);

        if($user = Session::get("index_user")){
            $uid = $user["id"];
            $order = Db::name('article_order')->where(['aid'=>$article['id'],'uid'=>$uid,'status'=>2])->find();
            if($order){

                View::assign('order',$order);
            }else{
                View::assign('order',0);
            }
        }else{

            View::assign('order',0);
        }
        View::assign('title',$article['title']);

        // 上一篇文章和下一篇文章
        $pre_article = Db::name("article")->where("id","<",$id)->order("id DESC")->find();
        $next_article = Db::name("article")->where("id",">",$id)->order("id ASC")->find();

        View::assign('pre_article',$pre_article);

        View::assign('next_article',$next_article);

        return view('read');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {


    }

    public function cache_clear()
    {
        Cache::clear();
    }
}
